home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / ispell-wrapper < prev    next >
Encoding:
Text File  |  2006-12-19  |  7.4 KB  |  282 lines

  1. #!/usr/bin/perl -w
  2.  
  3. use Debian::DictionariesCommon q(:all);
  4. use Getopt::Long;
  5. Getopt::Long::Configure(pass_through,no_auto_abbrev);
  6.  
  7. # Autoflush output buffers
  8. $|=1;
  9.  
  10. # Function to convert ISO-8859-1 (latin1) accented characters to
  11. # non-accented one.  Of course, this only works for west European
  12. # languages.  We might try to find a more general solution based on
  13. # the current locale character set.
  14.  
  15. sub isoconv {
  16.   my $s = shift;
  17.   $s =~ y{A-Z┴╔═╙┌▌ßΘφ≤·²└╚╠╥┘αΦ∞≥∙┬╩╬╘█ΓΩε⌠√─╦╧╓▄Σδ∩÷▀ⁿ ├╟╨╤╒πτ±⌡┼╞╪σµ°}
  18.          {a-zaeiouyaeiouyaeiouaeiouaeiouaeiouaeiouaeiosuyacdnoacnoaeoaeo};
  19.   return $s;
  20. }
  21.  
  22. #
  23. # Function to try getting $lang after emacsen name 
  24. #
  25.  
  26. sub try_emacsen () {
  27.     return unless ( $emacsen );
  28.     my @available_emacsen = ();
  29.     foreach $k (keys %$dictionaries) {        
  30.     my $language = $dictionaries->{$k}; 
  31.     my $hashname = $language->{"hash-name"};
  32.     my $emacsenname = exists $language->{"emacsen-name"} ?
  33.         $language->{"emacsen-name"} : $hashname;
  34.     return $k if ( lc($emacsen) eq lc($emacsenname) );
  35.     push @available_emacsen,$emacsenname;
  36.     }
  37.     print STDERR "Available emacs dict names are:\n";
  38.     foreach ( sort @available_emacsen ){
  39.     print STDERR "    $_\n";
  40.     }
  41.     die "$0: Selected emacs dict name [$emacsen] 
  42. does not match any of the available values\n";
  43. }
  44.  
  45. #
  46. # Function to try getting $lang after $regexp 
  47. #
  48.  
  49. sub try_regexp () {
  50.     return unless $regexp;
  51.     my @regexp_matches    = ();
  52.     $regexp = isoconv ($regexp);
  53.     foreach $key ( keys %$dictionaries ) {
  54.     $_ = isoconv ( $key );
  55.     push (@regexp_matches, $key)
  56.         if /$regexp/;
  57.     }
  58.     if (scalar @regexp_matches == 1){
  59.     return $regexp_matches[0];
  60.     } elsif ( scalar @regexp_matches == 0 ) {
  61.     die "$0: No installed language matched `$regexp'\n";
  62.     } else {
  63.     die ("$0: More than one installed languages matched `$regexp':\n  "
  64.          . join ("\n  ", @regexp_matches) . "\n");
  65.     }
  66. }
  67.  
  68. # --------------------------------------------------------------------
  69. # Now the main program
  70. # --------------------------------------------------------------------
  71.  
  72. my $lang           = '';
  73. $dictionaries      = loaddb ("ispell");
  74.  
  75. # In the POD section below there is an extensive description on the
  76. # priority order for determining the ispell language.
  77.  
  78. $emacsen    = '';
  79. $regexp     = '';
  80. my $dryrun  = '';
  81.  
  82. GetOptions ('emacs=s'    => \$emacsen,
  83.         'language=s' => \$regexp,
  84.         'dry-run'    => \$dryrun);
  85.  
  86. die " ispell-wrapper is a wrapper to ispell, but ispell is not installed.\n"
  87.     unless ( -x "/usr/bin/ispell" );
  88.  
  89. $regexp = $ENV{ISPELLDEFAULT}
  90.   if not $regexp;
  91.  
  92. $lang = &try_emacsen ()
  93.     ||  &try_regexp ()
  94.     ||  &getuserdefault ()
  95.     ||  &getsysdefault ();
  96.  
  97. print STDERR " Warning: --language=$regexp will be overriden by 
  98.           --emacs=$emacsen setting\n\n"
  99.     if ( defined $lang && $regexp && $emacsen );
  100.  
  101. # Finally, the ispell command will be called with the appropriate -d
  102. # and -w options.
  103.  
  104. my $d_option    = "";
  105. my $w_option    = "";
  106. my $T_option    = "";
  107. my $ispell_args = "";
  108. my $cli_opts    = "";
  109.  
  110. if (defined $lang) {
  111.   my %langhash = %{$dictionaries->{$lang}};
  112.   $d_option = "-d " . $langhash{"hash-name"}
  113.     if exists $langhash{"hash-name"};
  114.   $w_option = '-w "' . $langhash{"additionalchars"} . '"'
  115.     if exists $langhash{"additionalchars"};
  116.   $T_option = $langhash{"extended-character-mode"}
  117.     if exists $langhash{"extended-character-mode"};
  118.   $ispell_args = $langhash{"ispell-args"}
  119.     if exists $langhash{"ispell-args"};
  120.  
  121.   foreach ( split('\s+',$ispell_args) ) {
  122.       # No d_option if already in $ispell_args
  123.       $d_option = "" if /^\-d/; 
  124.   }
  125.   
  126.   # Strip leading ~ from Extended-Character-Mode. 
  127.   
  128.   $T_option=~s/^~//;
  129.   
  130.   # If Extended-Character-Mode is defined add leading -T
  131.   
  132.   if ( $T_option ne "" ){
  133.       $T_option='-T ' . $T_option;
  134.   }
  135.   $ispell_wrapper_args = "$d_option $w_option  $T_option $ispell_args";
  136. }
  137.  
  138. # Ignore $lang results if -d is set from commandline
  139.  
  140. foreach ( @ARGV ) { 
  141.     if (/^\-d/){
  142.     $ispell_wrapper_args = "";
  143.     }
  144.     $cli_opts .= " $_";
  145. }
  146.  
  147. print STDERR "Warning: \'$lang\' values overriden with \'$cli_opts\'\n"
  148.     if ( not $ispell_wrapper_args && defined $lang );
  149.  
  150. $command_to_run="ispell $ispell_wrapper_args $cli_opts";
  151.  
  152. if ( $dryrun ){
  153.     print "--\n$command_to_run\n--\n";
  154. } else {
  155.     exec $command_to_run;
  156. }
  157.  
  158. # Local Variables:
  159. # perl-indent-level: 2
  160. # End:
  161.  
  162. __END__
  163.  
  164. =head1 NAME
  165.  
  166. B<ispell-wrapper> - smart wrapper for ispell
  167.  
  168. =head1 SYNOPSIS
  169.  
  170.  ispell-wrapper [--emacs=name] [--language=regexp] [--dry-run] [ispell options] file
  171.  
  172.    Options (all long only options):
  173.     --emacs=name           Set the language to use by emacs dict name
  174.     --language=regexp      Set the language to use by name
  175.     --dry-run              Only show what would have done
  176.  
  177. =head1 DESCRIPTION
  178.  
  179. B<ispell-wrapper> is a wrapper script for ispell intended to be used
  180. in a Debian system in conjunction with the infrastructure introduced by
  181. the dictionaries-common package. Option --dry-run will show the string
  182. to be run without doing anything else.
  183.  
  184. It automatically sets the B<-d>, B<-w>, and B<-T> options to ispell as a
  185. function of the chosen language.  Of course, this only works for dictionary
  186. packages that comply with the above mentioned Policy.
  187.  
  188. Here is how the language is defined (in order of priority):
  189.  
  190. =over
  191.  
  192. =item 1)
  193.  
  194. By matching the emacs dict name given in --emacs option to the name of 
  195. one of the emacs dicts names provided by installed languages in the 
  196. system. This match must be exact (although is case insensitive). 
  197. Note that this will override any value given in the --language option.
  198.  
  199. =item 2)
  200.  
  201. By matching the regexp given in option --language to the list of
  202. installed languages in the system.
  203.  
  204. =item 3)
  205.  
  206. By matching the regexp stored in the environment variable
  207. ISPELLDEFAULT to the list of installed languages in the system.
  208.  
  209. =item 4)
  210.  
  211. By using the value stored in the user-specific file ~/.ispell-default
  212. (use select-default-iwrap(1) to set it).
  213.  
  214. =item 5)
  215.  
  216. By using the value stored in the site-wide file
  217. /etc/dictionaries-common/ispell-default (use select-default-ispell(8)
  218. as superuser to set it).
  219.  
  220. =back
  221.  
  222. Note: regexp matches are case-insensitive and the ISO-8859-1 special
  223. characters are transformed into their ASCII equivalents.  German
  224. ess-zet is equivalent to the character "s" and the ae ligature to the
  225. character "e".
  226.  
  227. =head1 EXAMPLE
  228.  
  229. Let us say that the following dictionaries are installed in the system
  230. (as appearing in the Debconf question at installation time):
  231.  
  232.     castellano (Spanish TeX mode)
  233.     castellano8 (Spanish 8 bit)
  234.     portuguΩs (European Portuguese)
  235.     portuguΩs brasileiro (Brazilian Portuguese)
  236.  
  237. Choosing the regexp (either in the --language option or in the
  238. environment variable ISPELLDEFAULT) to be "span" will yield an error,
  239. since two languages will match ("castellano" and "castellano8").
  240. However, if the regexp is "span.*8", the language "castellano8
  241. (Spanish 8 bit)" will be chosen.
  242.  
  243. =head1 ENVIRONMENT
  244.  
  245. =over
  246.  
  247. =item ISPELLDEFAULT
  248.  
  249. Regexp that matches the name of the default language to use, if no
  250. --language option is given.
  251.  
  252. =back
  253.  
  254.  
  255. =head1 FILES
  256.  
  257. =over
  258.  
  259. =item $HOME/.ispell-default
  260.  
  261. Contains the name of the language to use, if no --language option is
  262. given or if the ISPELLDEFAULT environment variable is not set.  This
  263. is a user-specific choice.
  264.  
  265. =item /etc/dictionaries-common/ispell-default
  266.  
  267. Name of the language to use when everything above is not set. This is
  268. a system-wide setting.
  269.  
  270. =back
  271.  
  272.  
  273. =head1 SEE ALSO
  274.  
  275. select-default-ispell(8), select-default-iwrap(1)
  276.  
  277. =head1 AUTHORS
  278.  
  279. Rafael Laboissiere
  280.  
  281. =cut
  282.